package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.ToolInfoExcelEx;
import com.be.beadmin.device.entity.ToolInfo;
import com.be.beadmin.device.dto.output.ToolInfoEx;
import com.be.beadmin.device.repository.ToolInfoRepository;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.ToolInfoService;
import com.be.beadmin.device.dto.input.ToolInfoQueryCriteria;
import com.be.beadmin.device.dto.input.ToolInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author sly
* @date 2023-08-26
**/
@RestController
public class ToolInfoController {

    @Autowired
    private  ToolInfoService toolInfoService;
    @Autowired
    private ToolInfoRepository toolInfoRepository;


    @PostMapping(value = "/device/getToolInfoByPage")
    public ResultModel getToolInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(toolInfoService.getToolInfoByPage(pageEntity)) ;
    }

    @PostMapping("/device/addToolInfo")
    public ResultModel addToolInfo(@RequestBody ToolInfoDto toolInfoDto){
        toolInfoService.addToolInfo(toolInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delToolInfo")
    public ResultModel delToolInfo(@RequestBody ToolInfoDto toolInfoDto){
        toolInfoService.delToolInfo(toolInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateToolInfo")
    public ResultModel updateToolInfo(@RequestBody ToolInfoDto toolInfoDto){
        toolInfoService.updateToolInfo(toolInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportToolInfo")
    public void exportToolInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ToolInfoEx> toolInfoPage = toolInfoService.getToolInfoByPage(pageEntity);
        List<ToolInfoEx> toolInfoExes = toolInfoPage.getContent();
        List<ToolInfoExcelEx> toolexcel=toolInfoExes.stream().map(item->{
            ToolInfoExcelEx excelEx=new ToolInfoExcelEx();
            BeanUtils.copyProperties(item,excelEx);
            return excelEx;
        }).collect(Collectors.toList());;
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ToolInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), ToolInfoExcelEx.class).sheet("sheet1").doWrite(toolexcel);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), ToolInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(toolexcel);
    }

    // 筛选功能
    @ApiOperation("根据领用日期获取项目信息")
    @PostMapping(value = "/project/getToolInfoByTakeawayDate")
    public ResultModel getToolInfoByTakeawayDate(@RequestParam Date takeawayDate) {
        return ResultModel.success(toolInfoService.getToolInfoByTakeawayDate(takeawayDate));
    }

    @ApiOperation("根据报废日期获取项目信息")
    @PostMapping(value = "/project/getToolInfoByScrappedDate")
    public ResultModel getToolInfoByScrappedDate(@RequestParam Date scrappedDate) {
        return ResultModel.success(toolInfoService.getToolInfoByScrappedDate(scrappedDate));
    }

    @ApiOperation("根据采购日期获取项目信息")
    @PostMapping(value = "/project/getToolInfoByContractDate")
    public ResultModel getToolInfoByContractDate(@RequestParam Date contractDate) {
        return ResultModel.success(toolInfoService.getToolInfoByContractDate(contractDate));
    }

    @ApiOperation("获取设备类别字典")
    @PostMapping("/device/getCategoryDict")
    public ResultModel getCategoryDict(){
        String dictTypeId = toolInfoRepository.findDictTypeId("设备类别");
        List<String> categoryDict = toolInfoRepository.findKeyNameByDictTypeId(dictTypeId);
        return ResultModel.success(categoryDict);
    }
}